﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using BLL;

public partial class BookManage : System.Web.UI.Page
{
    private static IList<BookInfo> bookList;
    private static Book book = new Book();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            bookList = book.GetAllBooks().ToList();
            BookGrid.DataSource = bookList;
            BookGrid.DataBind();
        }
    }

    protected void UndealOrder_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            try
            {
                
                book.DisableBook(bookList[e.Item.ItemIndex].ISBN);
                Response.Write("<script>alert('下架成功:')</script>");
                bookList = book.GetAllBooks().ToList();
                BookGrid.DataSource = bookList;
                BookGrid.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write("<script>alert('下架失败: " + ex.Message + "')</script>");
            }

        }
        if (e.CommandName == "Edit")
        {

            ISBN.Text = bookList[e.Item.ItemIndex].ISBN;
            BookName.Text = bookList[e.Item.ItemIndex].BookName;
            Author.Text = bookList[e.Item.ItemIndex].BookAuthor;
            ReleaseDate.Text = bookList[e.Item.ItemIndex].BookReleaseTime.ToShortDateString();
            Picture.Text = bookList[e.Item.ItemIndex].BookPicture;
            BookDesc.Text = bookList[e.Item.ItemIndex].BookDesc;
            UnitPrice.Text = bookList[e.Item.ItemIndex].BookPrice.ToString();
            RemainNum.Text = bookList[e.Item.ItemIndex].BookRemainNum.ToString();
            SubEditButton.Enabled = true;
            AddButton.Enabled = false;
            ISBN.ReadOnly = true;
        }
        
    }
    protected void AddButton_Click(object sender, EventArgs e)
    {
        try
        {
            
            book.AddBook(ISBN.Text,
                           BookName.Text,
                           Author.Text,
                           BookDesc.Text,
                           Picture.Text,
                           Convert.ToDouble(UnitPrice.Text),
                           Convert.ToInt32(RemainNum.Text),
                           Convert.ToDateTime(ReleaseDate.Text));
            Response.Write("<script>alert('添加成功:')</script>");
            bookList = book.GetAllBooks().ToList();
            BookGrid.DataSource = bookList;
            BookGrid.DataBind();
            ISBN.Text = "";
            BookName.Text = "";
            Author.Text = "";
            ReleaseDate.Text = "";
            Picture.Text = "";
            BookDesc.Text = "";
            UnitPrice.Text = "";
            RemainNum.Text = "";
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('添加失败: " + ex.Message + "')</script>");
        }
        
    }
    protected void SubEditButton_Click(object sender, EventArgs e)
    {
        try
        {

            book.ModifyBook(ISBN.Text,
                BookName.Text,
                Author.Text,
                BookDesc.Text,
                Picture.Text,
                Convert.ToDouble(UnitPrice.Text),
                Convert.ToInt32(RemainNum.Text),
                Convert.ToDateTime(ReleaseDate.Text));

            
            Response.Write("<script>alert('修改成功:')</script>");
            bookList = book.GetAllBooks().ToList();
            BookGrid.DataSource = bookList;
            BookGrid.DataBind();
            SubEditButton.Enabled = false;
            AddButton.Enabled = true;
            ISBN.ReadOnly = false;
            ISBN.Text = "";
            BookName.Text = "";
            Author.Text = "";
            ReleaseDate.Text = "";
            Picture.Text = "";
            BookDesc.Text = "";
            UnitPrice.Text = "";
            RemainNum.Text = "";
        }

        catch(Exception ex)
        {
            Response.Write("<script>alert('修改失败: " + ex.Message + "')</script>");
        }
    }
}
